package cn.cvs.springbootmybatisplus.controller;

import cn.cvs.springbootmybatisplus.entity.Book;
import cn.cvs.springbootmybatisplus.service.BookService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;
    @RequestMapping
    public String book(Model model){
        List<Book> list = bookService.list();
        model.addAttribute("list",list);
        return "index";
    }

    @PostMapping
    public String searchBooks(Model model,Book book){
      List<Book> list=  bookService.searchBooks(book);
      model.addAttribute("list",list);
      model.addAttribute("name",book.getName());
      model.addAttribute("category",book.getCategory());
      model.addAttribute("author",book.getAuthor());
      return "index";
    }

    @RequestMapping("/page")
    public String page(@RequestParam(defaultValue = "1") Integer pageNum, Model model){
        IPage<Book> page=bookService.getPage(pageNum);
        model.addAttribute("page",page);
        return "index";
    }

    @RequestMapping("/addOrUpdate")
    public String addOrUpdate(Book book,Model model){
        bookService.saveOrUpdate(book);
        return "redirect:/index";
    }


    @PostMapping("/add")
    public String add(){
        return "/add";
    }
}
